<?php

namespace App\Remote\MoreDo;

use App\Library\FeiShu\SendFs;
use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class Remote extends \App\Remote\Remote
{
    /**
     * 设置请求超时时长
     *
     * @var int
     */
    public static int $requestTimeOut = 30;

    static function getBaseUri(): string
    {
        // TODO: Implement getBaseUri() method.
        return "http://api.moredo.cn:9000";
    }

    /**
     * 基础请求头构建
     *
     * @return array
     * @throws \Exception
     */
    public static function buildBasicHeaders(): array
    {
        return [
            'Content-Type' => "application/json"
        ]; // TODO: Change the autogenerated stub
    }

    public static function registerResponseHandle(): Closure
    {
        return function ($responseHttpCode, $responseOriginalContents) {
            if (
                $responseHttpCode == ResponseAlias::HTTP_OK
                &&
                $parsedResponse = jsonToArr($responseOriginalContents, [])
            ) {
                $ret = attributeGet($parsedResponse, "ret", null);

                if ($ret == 1 && Str::contains(attributeGet($parsedResponse, "msg", ""), "token")) {
                    //删除失效的token
                    Cache::store("redis")->delete("moreDo:authToken");
                }

                if ($ret == 200) {
                    return attributeGet($parsedResponse, "data", "");
                }
            }
            throw new \Exception($responseOriginalContents);
        }; // TODO: Change the autogenerated stub
    }

}
